<template>
  <div class="login-container">
    <div class="login-box">
      <h2>欢迎登录</h2>
      <el-form :model="form" :rules="rules" ref="loginForm">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="用户名" prefix-icon="User"/>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="密码" prefix-icon="Lock" show-password/>
        </el-form-item>
        <div class="form-actions">
          <el-button type="primary" :style="{backgroundColor: '#4f46e5'}" @click="handleLogin" class="login-btn">登录
          </el-button>
        </div>
        <div class="form-links">
          <router-link to="/register" class="link">注册账号</router-link>
          <router-link to="/forgot-password" class="link">忘记密码？</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import  { upost } from "@/api/fetch";

export default {
  name: 'LoginView',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
        password: [{required: true, message: '请输入密码', trigger: 'blur'}]
      }
    }
  },
  methods: {
    async handleLogin() {
      try {
        const valid = await this.$refs.loginForm.validate()
        if (valid) {
          // eslint-disable-next-line no-debugger
          debugger
          let paramInfo = {
            "username": this.form.username,
            "passwordHash": this.form.password
          }
          upost('/client-user/user/pc/login', paramInfo).then((res) => {
            console.log(res)
            if (res) {
              if (res.code === '200' && res.result) {
                localStorage.setItem('token', res.result.token)
                // 更新Vuex中的认证状态
                this.$store.dispatch('user/login', {
                  username: this.form.username
                }).then(() => {
                  this.$message.success('登录成功')
                  this.$router.push('/home')
                })
              }
            }
          })
        }
      } catch (error) {
        this.$message.error('登录失败，请重试')
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  min-height: 100vh;
  background-image: url('../static/img/【哲风壁纸】一拳超人-动漫-战斗.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 1rem;
}

.login-box {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
  background-color: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(5px);
  border-radius: 0.5rem;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
  margin-right: 5%;
}

h2 {
  text-align: center;
  color: #4f46e5;
  margin-bottom: 2rem;
  font-size: clamp(1.5rem, 4vw, 2rem);
  text-shadow: 0 0 5px rgba(255, 255, 255, 0.8);
}

.form-actions {
  margin-top: 1.5rem;
}

.login-btn {
  width: 100%;
  padding: 0.75rem;
  font-size: clamp(0.875rem, 2vw, 1rem);
}

.form-links {
  display: flex;
  justify-content: space-between;
  margin-top: 1rem;
}

.link {
  color: #4f46e5;
  text-decoration: none;
  font-size: clamp(0.75rem, 2vw, 0.875rem);
  font-weight: bold;
}

.link:hover {
  text-decoration: underline;
}

@media (max-width: 768px) {
  .login-container {
    justify-content: center;
  }
  
  .login-box {
    margin-right: 0;
    background-color: rgba(255, 255, 255, 0.8);
  }
}

@media (max-width: 480px) {
  .login-box {
    padding: 1.5rem;
  }

  .form-links {
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
  }
}
</style>